// page/index/index.js
// pages/travel/travel.js
var httpUtil = require('../../utils/httpUtil.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [],
    sysW: null,
    goods:{},
    money:0.00,
    lastDay: null,
    firstDay: null,
    goodsTravelSpecificationList:[],
    weekArr: ['日', '一', '二', '三', '四', '五', '六'],
    year: null,
    num:true,
    currIndex:0,
    curIdx:0,
    amount:1,
    ticketNum:[
      { name:'成人',numTitle: '成人（15岁以上）', amount: 1, djNum: 0.00 }, 
      { name:'儿童', numTitle: '儿童（4-14岁）', amount: 0, djNum: 0.00 }, 
      { name:'婴儿', numTitle: '婴儿（0-3岁）', amount: 0, djNum: 0.00 }
      ],
    hotelInfo:[
      { hotelName: '当地酒店1', hotelMoney: 239, urlImg: '../images/shop-no.png', urlSelImg: '../images/shop-yes.png' }, { hotelName: '当地酒店2', hotelMoney: 239, urlImg: '../images/shop-no.png', urlSelImg: '../images/shop-yes.png' }, { hotelName: '当地酒店3', hotelMoney: 239, urlImg: '../images/shop-no.png', urlSelImg: '../images/shop-yes.png' }
    ],
    dayStyle: [
            { month: 'current', day: 0, color: '#eee' },

      { month: 'current', day: 1, color: '#eee'},
      { month: 'current', day: 2, color: '#eee' },
      { month: 'current', day: 3, color: '#eee' },
      { month: 'current', day: 4, color: '#eee' },
      { month: 'current', day: 5, color: '#eee' },
      { month: 'current', day: 6, color: '#eee' },
      { month: 'current', day: 7, color: '#eee' },
      { month: 'current', day: 8, color: '#eee' },
      { month: 'current', day: 9, color: '#eee' },
      { month: 'current', day: 10, color: '#eee' },
      { month: 'current', day: 11, color: '#eee' },
      { month: 'current', day: 12, color: '#eee' },
      { month: 'current', day: 13, color: '#eee' },
      { month: 'current', day: 14, color: '#eee' },
      { month: 'current', day: 15, color: '#eee' },
      { month: 'current', day: 16, color: '#eee' },
      { month: 'current', day: 17, color: '#eee' },
      { month: 'current', day: 18, color: '#eee' },
      { month: 'current', day: 19, color: '#eee' },
      { month: 'current', day: 20, color: '#eee' },
      { month: 'current', day: 21, color: '#eee' },
      { month: 'current', day: 22, color: '#eee' },
      { month: 'current', day: 23, color: '#eee' },
      { month: 'current', day: 24, color: '#eee' },
      { month: 'current', day: 25, color: '#eee' },
      { month: 'current', day: 26, color: '#eee' },
      { month: 'current', day: 27, color: '#eee' },
      { month: 'current', day: 28, color: '#eee' },
      { month: 'current', day: 29, color: '#eee' },
      { month: 'current', day: 30, color: '#eee' },
      { month: 'current', day: 31, color: '#eee' },
      { month: 'current', day: 32, color: '#eee' }
    ], dayStyles: [
      { month: 'current', day: 0, color: '#eee' },
      { month: 'current', day: 1, color: '#eee' },
      { month: 'current', day: 2, color: '#eee' },
      { month: 'current', day: 3, color: '#eee' },
      { month: 'current', day: 4, color: '#eee' },
      { month: 'current', day: 5, color: '#eee' },
      { month: 'current', day: 6, color: '#eee' },
      { month: 'current', day: 7, color: '#eee' },
      { month: 'current', day: 8, color: '#eee' },
      { month: 'current', day: 9, color: '#eee' },
      { month: 'current', day: 10, color: '#eee' },
      { month: 'current', day: 11, color: '#eee' },
      { month: 'current', day: 12, color: '#eee' },
      { month: 'current', day: 13, color: '#eee' },
      { month: 'current', day: 14, color: '#eee' },
      { month: 'current', day: 15, color: '#eee' },
      { month: 'current', day: 16, color: '#eee' },
      { month: 'current', day: 17, color: '#eee' },
      { month: 'current', day: 18, color: '#eee' },
      { month: 'current', day: 19, color: '#eee' },
      { month: 'current', day: 20, color: '#eee' },
      { month: 'current', day: 21, color: '#eee' },
      { month: 'current', day: 22, color: '#eee' },
      { month: 'current', day: 23, color: '#eee' },
      { month: 'current', day: 24, color: '#eee' },
      { month: 'current', day: 25, color: '#eee' },
      { month: 'current', day: 26, color: '#eee' },
      { month: 'current', day: 27, color: '#eee' },
      { month: 'current', day: 28, color: '#eee' },
      { month: 'current', day: 29, color: '#eee' },
      { month: 'current', day: 30, color: '#eee' },
      { month: 'current', day: 31, color: '#eee' },
      { month: 'current', day: 32, color: '#eee' }
    ],
  },

  //获取日历相关参数
  dataTime: function() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var months = date.getMonth() + 1;

    //获取现今年份
    this.data.year = year;

    //获取现今月份
    this.data.month = months;

    //获取今日日期
    this.data.getDate = date.getDate();
  
    //最后一天是几号
    var d = new Date(year, months, 0);
    this.data.lastDay = d.getDate();

    //第一天星期几
    let firstDay = new Date(year, month, 1);
    this.data.firstDay = firstDay.getDay();
  },

  onLoad: function(options) {
    var that=this;
    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2];  //上一个页面
     this.dataTime();
    this.setData({
      goods: prevPage.data.goods
    })
    that.getSpecification(that.data.goods, this.data.year + "-" + this.data.month+"-"+this.data.getDate)

     //根据得到今月的最后一天日期遍历 得到所有日期
    for (var i = 1; i < this.data.lastDay + 1; i++) {
      this.data.arr.push(i);
    }
    var res = wx.getSystemInfoSync();
    this.setData({
      sysW: res.windowHeight / 12, //更具屏幕宽度变化自动设置宽度
      marLet: this.data.firstDay,
      arr: this.data.arr,
      year: this.data.year,
      getDate: this.data.getDate,
      month: this.data.month
     });
    that.getSpecificationMonth(that.data.goods, this.data.year, this.data.month,0);
  },
  getSpecification: function (goods,date){
    var that = this;
    httpUtil.getHttp("/goods_travel/get/" + goods.id + "/" + goods.goodsType+"/specification?date="+date, {}, function (res) {
      that.setData({
       goodsTravelSpecificationList: res      
      })
      console.log(res);
      if(res.length>0){
        that.getSpecificationNum(res[that.data.currIndex]);
      }
    })
  },
  getSpecificationNum: function (specification){
    var that = this;
      that.setData({
         ticketNum: [
           { name: '成人', numTitle: '成人（15岁以上）', amount: 1, djNum: specification.adultMoney }, { name: '儿童', numTitle: '儿童（4-14岁）', amount: 0, djNum: specification.teensMoney }, { name: '婴儿', numTitle: '婴儿（0-3岁）', amount: 0, djNum: specification.babyMoney }
        ],
        money: specification.adultMoney
      })

    
  },
  clickday:function(res){
    var date = new Date();
    this.data.getDate = date.getDate();
    this.setData({
      getDate:res.currentTarget.dataset.item     })
    var that=this;
    var da=that.data.year + "-" + that.data.month + "-" + that.data.getDate
    that.getSpecification(that.data.goods, da);

   },
  selSetmeal:function(res){
    var that=this;
    that.setData({
      currIndex:res.currentTarget.dataset.index
    })
    that.getSpecificationNum(that.data.goodsTravelSpecificationList[that.data.currIndex]);
  },
  reduceNum:function(res){
    var that = this;
    var index = res.currentTarget.dataset.num;
    if (that.data.ticketNum[index].amount>=1){
      var sel = "ticketNum[" + index + "].amount"
      var money = that.data.money - that.data.ticketNum[index].djNum < 0 ? 0 : that.data.money - that.data.ticketNum[index].djNum;

      that.setData({
        [sel]: that.data.ticketNum[index].amount - 1,
        money: parseFloat(money.toFixed(2))

      })
     }
  },
  addNum:function(res){
    var that = this;
    var index = res.currentTarget.dataset.num;
    var sel = "ticketNum[" + index + "].amount"
    var money = that.data.money + that.data.ticketNum[index].djNum;


    that.setData({
      [sel]: that.data.ticketNum[index].amount +1,
      money: parseFloat(money.toFixed(2))
    })
  },
  selHotel:function(res){
    var that = this;
    that.setData({
      curIdx: res.currentTarget.dataset.in,
    })
  },
  nextLoad:function(res){
    var that = this;
    if (that.data.money==0){
      wx.showToast({
        title: '请选择规格人数',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    wx:wx.navigateTo({
      url: '../orderSure/orderSure',
    })
  },
  //给点击的日期设置一个背景颜色
  dayClick: function (event) {
    console.log(event)
    var that=this;
    var dayStyle = that.data.dayStyle;
    var s = 0;
    for (var i = 0; i < dayStyle.length; i++) {
      if (dayStyle[i].day == event.detail.day) {
        s = 1;
      }else{
        dayStyle[i].background = "";

      }
    }
    if (s == 1) {
      return;
    }
    that.setData({
      dayStyle: dayStyle
    })
   
     let clickDay = event.detail.day;
    let changeDay = `dayStyle[0].day`;
    let changeBg = `dayStyle[0].background`;
    this.setData({
      [changeDay]: clickDay,
      [changeBg]: "#84e7d0"
    })
  
     
    var da = event.detail.year + "-" + event.detail.month + "-" + event.detail.day
    that.data.year = event.detail.year;
    that.data.month = event.detail.month;
    that.data.getDate = event.detail.day;
 
    that.getSpecification(that.data.goods, da);

  }, dateChange: function (event) {
    var that = this;

    console.log(event.detail);
    that.getSpecificationMonth(that.data.goods, event.detail.currentYear, event.detail.currentMonth,1);

  }, prev: function (event) {
    var that = this;

    console.log(event.detail);
    that.getSpecificationMonth(that.data.goods, event.detail.currentYear, event.detail.currentMonth,1);

  }, next: function (event) {
    var that=this;
    console.log(event.detail);
    that.getSpecificationMonth(that.data.goods, event.detail.currentYear, event.detail.currentMonth,1);
  }, getSpecificationMonth: function (goods, year, month,s) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/specification/" + goods.id + "/" + goods.goodsType + "/" + month + "/" + year, {}, function (res) {
      var dayStyles = that.data.dayStyles;
      var list =[];

      for(var i = 0; i < dayStyles.length;i++){
  
        if (res.indexOf(dayStyles[i].day+"")==-1){
          dayStyles[i].background = "";
          dayStyles[i].color = "#eee";
          list.push(dayStyles[i])
      }else{
        
          if (that.data.year == year&&that.data.month == month&&dayStyles[i].day == that.data.getDate) {
            dayStyles[i].background ="#84e7d0";
            dayStyles[i].color = "#4a4f74";
            list.push(dayStyles[i]);
          }
      }
        
      }
      that.setData({
        dayStyle: list
      })
      
    })
  },
})